//
//  ViewController.m
//  组播接收
//
//  Created by lenkeng on 2019/9/16.
//  Copyright © 2019 lenkeng. All rights reserved.
//

#import "ViewController.h"
#import "GCDAsyncUdpSocket.h"

@interface ViewController ()<GCDAsyncUdpSocketDelegate>
@property (nonatomic, strong)GCDAsyncUdpSocket* udp_server_sock;
@property (nonatomic, strong)UITextView* textView;

@end

@implementation ViewController
- (void)createUDPServer {
    _udp_server_sock = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    NSError* bindError = nil;
    [_udp_server_sock bindToPort:8100 error:&bindError];
    if (bindError) {
        NSLog(@"bindError:%@", bindError);
    }
    
    NSError* broadError;
    [_udp_server_sock enableBroadcast:YES error:&broadError];
    if (broadError) {
        NSLog(@"error:%@", broadError);
    }
    
    NSError* mError = nil;
    [_udp_server_sock joinMulticastGroup:@"239.0.0.2" error:&mError];
    if (mError) {
        NSLog(@"mError:%@", mError);
    }

    NSError* receiveError = nil;
    [_udp_server_sock beginReceiving:&receiveError]; //开始接收数据，一定要否则后面无法接收数据
    if (receiveError) {
        NSLog(@"receiveError:%@", receiveError);
    }
    
}

-(void)loadData{
    NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http:www.baidu.com"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    //    [webView loadRequest:request];
    
    NSOperationQueue* queue = [[NSOperationQueue alloc] init];

        [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data,NSError *connectionError) {
            // 需要将二进制数据转换成html的字符串
            // 注：NSData转为字符串没有类方法
            // ENCODING字符编码,如果没有特殊要求,在iOS中统一使用UTF8
            NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"%@", html);
            
            //用webView加载html
            /**
             baseURL:http://www.baidu.com
             使用的时候URL就可以只传后半部分就行
             */
         
            
        }];

}
    
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self createUDPServer];
    _textView = [[UITextView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_textView];
    _textView.font = [UIFont systemFontOfSize:20];
    // Do any additional setup after loading the view.
}

-(void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext{
    
    NSString* dataString = [NSString stringWithFormat:@"ReceiveData = %@, fromAddress = %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding],[[NSString alloc] initWithData:address encoding:NSUTF8StringEncoding]];//从此可以获取服务端回应的ip和端口 用于后面的tcp连接NSLog(@"Adress = %@ %i",host,port];
    //    [self.view makeToast:[NSString stringWithFormat:dataString]];
    _textView.text = [[_textView.text stringByAppendingString:dataString] stringByAppendingString:@"\n"];
    
    
}


@end
